var type = "";
var way = "wechat";
$(function () {
    var channel = getQueryString("channel");
    $(".back-wrapper").click(function () {
        location.href = "./mine.html?channel="+channel;
    });
    $(".home-wrapper").click(function () {
        location.href = "../index.html?channel="+channel;
    });
    $(".close-wrapper").click(function () {
        $(".mask").hide();
    });
    $(".pay-method").click(function () {
        $(this).children(".check-box").addClass("active");
        $(this).siblings(".pay-method").children(".check-box").removeClass("active");
        way = $(this).attr("data-way");
    });
});

function openVip(t) {
    type = t;
    $(".mask").show();
}

function goOpen() {
    console.log(type,way);
    if(way == "wechat"){
        // alert("微信支付");
        $.ajax({
            type:'get',
            url:BASE_URl + "pay/apply/wechat?code=" + type,
            success:function (data) {
                if(data.success){
                    if (typeof WeixinJSBridge == "undefined") {
                        if (document.addEventListener) {
                            document.addEventListener("WeixinJSBridgeReady",onBridgeReady(), false);
                        } else if (document.attachEvent) {
                            document.attachEvent("WeixinJSBridgeReady", onBridgeReady());
                            document.attachEvent("onWeixinJSBridgeReady",onBridgeReady());
                        }
                    }else {
                        onBridgeReady(data.data);
                    }
                }else{
                    Toast(data.data.ercode);
                }
            }
        })
    }else if(way == "alipay"){
        // alert("支付宝支付");
        location.href = "/view/pay/apply/alipay?code="+type;
    }
}

// 微信支付
function onBridgeReady(param) {
    WeixinJSBridge.invoke("getBrandWCPayRequest", param, function(res) {
        if (res.err_msg == "get_brand_wcpay_request:ok") {
            // 支付成功
            Toast("支付成功");
            location.reload();
        }
        if (res.err_msg == "get_brand_wcpay_request:cancel") {
            // 取消支付
            Toast("支付已取消");
            $.ajax({
                type:'get',
                url:BASE_URl + "pay/cancel/wechat?orderid=" + param.package.split("=")[1],
                success:function (data) {}
            });
        }
        if (res.err_msg == "get_brand_wcpay_request:fail") {
            // 支付失败
            Toast("支付失败");
        }
    });
}